package com.boyunvision.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlUtil {
  public static byte[] requestImageUrl(String urlStr) throws IOException {
    HttpURLConnection connection = null;
    ByteArrayOutputStream outStream = null;
    InputStream in = null;
    try {
      URL url = new URL(urlStr);
      connection = (HttpURLConnection)url.openConnection();
      connection.setReadTimeout(20000);
      if (200 == connection.getResponseCode()) {
        outStream = new ByteArrayOutputStream();
        in = connection.getInputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = in.read(buffer)) != -1)
          outStream.write(buffer, 0, len); 
        return outStream.toByteArray();
      } 
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      assert outStream != null;
      outStream.close();
      assert in != null;
      in.close();
    } 
    return null;
  }
}
